package schoolbolg_gh326.fragment;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import schoolbolg_gh326.Adapter.PhotoAdapter;
import schoolbolg_gh326.Bean.Constants;
import schoolbolg_gh326.Bean.PhotoBean;
import schoolbolg_gh326.service.PhotoService;
import android.os.Bundle;
import android.app.Activity;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.schoolbolg_gh326.R;
import com.example.schoolbolg_gh326.R.layout;

/**
 * A simple {@link Fragment} subclass. Activities that contain this fragment
 * must implement the {@link FasongFragment.OnFragmentInteractionListener}
 * interface to handle interaction events. Use the
 * {@link FasongFragment#newInstance} factory method to create an instance of
 * this fragment.
 *
 */
public class PhotoFragment extends Fragment {
	private ListView lv;
	private View view;
	private Retrofit retrofit;
	private PhotoService service;
	private Call<List<PhotoBean>> getlist;
	private List<PhotoBean> list;
	private PhotoAdapter adapter;
	

	public PhotoFragment() {
		// Required empty public constructor
		retrofit =new Retrofit
				.Builder()
				.baseUrl(Constants.web)
				.addConverterFactory(GsonConverterFactory.create())
				.build();
	}



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		view= inflater.inflate(R.layout.fragment_photo, container,false);
		init();
		service = retrofit.create(PhotoService.class);
		getlist = service.getlist(1, "blogphoto", Constants.sessionID);
		getlist.enqueue(new Callback<List<PhotoBean>>() {
			
			@Override
			public void onResponse(Call<List<PhotoBean>> arg0,
					Response<List<PhotoBean>> arg1) {
				// TODO Auto-generated method stub
				if (arg1.isSuccessful()) {
					list = arg1.body();
					if (list !=null) {
						adapter = new PhotoAdapter(getActivity(), list);
						lv.setAdapter(adapter);
						
					}
					
				}
			}
			
			@Override
			public void onFailure(Call<List<PhotoBean>> arg0, Throwable arg1) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		return view;
	}



	private void init() {
		// TODO Auto-generated method stub
		lv = (ListView) view.findViewById(R.id.photo_listView);
	}

	// TODO: Rename method, update argument and hook method into UI event


	public void refresh() {
		// TODO Auto-generated method stub
		
	}
	


}
